package com.weixin.paleituo.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.weixin.paleituo.R;
import com.weixin.paleituo.base.BaseRecycleAdapter;
import com.weixin.paleituo.bean.ShippingAddressBean;
import com.weixin.paleituo.eventbus.CheckDeleteAddressEvent;
import com.weixin.paleituo.eventbus.CheckSetDefaultAddressEvent;
import com.weixin.paleituo.eventbus.CheckUpdateAddressEvent;
import com.weixin.paleituo.help.RecycleViewHolder;
import com.weixin.paleituo.util.UIUtils;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 购物车地址管理模块，收货地址列表Adapter
 *
 * @author fengzhen
 * @version v1.0, 2018/3/28
 */
public class ShippingAddressRecycleAdapter extends BaseRecycleAdapter<ShippingAddressBean> {
    public ShippingAddressRecycleAdapter(Context context, List<ShippingAddressBean> datas) {
        super(context, datas, R.layout.item_shipping_address);
    }

    @Override
    protected void setData(RecycleViewHolder holder, final ShippingAddressBean shippingAddressBean, int position) {
        ViewHolder viewHolder = (ViewHolder) holder;
        viewHolder.mTvUserItemShippingAddress.setText(shippingAddressBean.getContact());
        viewHolder.mTvTelItemShippingAddress.setText(shippingAddressBean.getPhone());
        viewHolder.mTvAddressItemShippingAddress.setText(shippingAddressBean.getDetailAddress());
        viewHolder.mTvChoiceItemShippingAddress.setEnabled(shippingAddressBean.isdefault());
        viewHolder.mImgDefaultItemShippingAddress.setVisibility(
                shippingAddressBean.isdefault() ? View.VISIBLE : View.GONE);
        viewHolder.mTvChoiceItemShippingAddress.setEnabled(!shippingAddressBean.isdefault());

        viewHolder.mTvChoiceItemShippingAddress.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EventBus.getDefault().post(new CheckSetDefaultAddressEvent(shippingAddressBean.getId()));
            }
        });
        viewHolder.mTvDeleteItemShippingAddress.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EventBus.getDefault().post(new CheckDeleteAddressEvent(shippingAddressBean.getId()));
            }
        });
        viewHolder.mTvEditItemShippingAddress.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EventBus.getDefault().post(new CheckUpdateAddressEvent(shippingAddressBean));
            }
        });
    }

    @Override
    public RecycleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(UIUtils.inflate(parent.getContext(), R.layout.item_shipping_address,parent,false));
    }

    static class ViewHolder extends RecycleViewHolder {
        @BindView(R.id.tv_user_item_shipping_address)
        TextView mTvUserItemShippingAddress;
        @BindView(R.id.tv_tel_item_shipping_address)
        TextView mTvTelItemShippingAddress;
        @BindView(R.id.img_default_item_shipping_address)
        ImageView mImgDefaultItemShippingAddress;
        @BindView(R.id.tv_address_item_shipping_address)
        TextView mTvAddressItemShippingAddress;
        @BindView(R.id.tv_choice_item_shipping_address)
        TextView mTvChoiceItemShippingAddress;
        @BindView(R.id.tv_delete_item_shipping_address)
        TextView mTvDeleteItemShippingAddress;
        @BindView(R.id.tv_edit_item_shipping_address)
        TextView mTvEditItemShippingAddress;

        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }
}
